<?php


namespace Service\AppService;
use Service\BaseService;

/**
 * 公共服务
 * @author
 */
class Common extends BaseService
{
    public static function msectime() 
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }
    
	//获取Y-m-d H:i:s
    public static function getDate() 
    {
        return date("Y-m-d H:i:s");
    }
    
    //格式化指定日期
    public static function getDateByTimeStamp($timestamp) 
    {
        return date("Y-m-d H:i:s",$timestamp);
    }
    
    /**
     * 时间搜索参数组装
     * @param array $param
     * @param string $filed
     * @param string $startKey
     * @param string $endKey
     */
    public static function getTimeStampParam( $param = [] , $filed = 'cd',$startKey = 'cd_start_time',$endKey = 'cd_end_time')
    {
        $start_time = strtotime($param[$startKey]??0);
        $end_time   = strtotime($param[$endKey]??0);
        $whereTime  = [
            'timeStr'   => '',
            'st'        => $start_time,
            'et'        => $end_time + 86399,
        ];
        if( !empty($start_time) && !empty($end_time) ) {
            $whereTime['timeStr'] = " {$filed} >= {$whereTime['st']} and {$filed} <= {$whereTime['et']}";
        } elseif ( empty($start_time) && !empty($end_time) ) {
            $whereTime['timeStr'] = " {$filed} <= {$whereTime['et']} ";
        } elseif ( !empty($start_time) && empty($end_time) ) {
            $whereTime['timeStr'] = " {$filed} >= {$whereTime['st']} ";
        }
        return $whereTime;
    }

    /**
     * @param array $param
     * @param string $filed
     * @param string $startKey
     * @param string $endKey
     * @return array
     */
    public static function getDateTimeParam( $param = [] , $filed = 'cd',$startKey = 'cd_start_time',$endKey = 'cd_end_time',$timestamp = false)
    {
        $start_time = $param[$startKey]??'';
        $end_time   = $param[$endKey]??'';
        $whereTime  = [
            'timeStr'   => '',
            'st'        => $timestamp ? strtotime($start_time) : date('Y-m-d H:i:s',strtotime($start_time)),
            'et'        => $timestamp ? strtotime($end_time)   : date('Y-m-d H:i:s',strtotime($end_time)),
        ];
        if( !empty($start_time) && !empty($end_time) ) {
            $whereTime['timeStr'] = " {$filed} >= '{$whereTime['st']}' and {$filed} <= '{$whereTime['et']}'";
        } elseif ( empty($start_time) && !empty($end_time) ) {
            $whereTime['timeStr'] = " {$filed} <= '{$whereTime['et']}' ";
        } elseif ( !empty($start_time) && empty($end_time) ) {
            $whereTime['timeStr'] = " {$filed} >= '{$whereTime['st']}' ";
        }
        return $whereTime;
    }
    
    /**
     * 将数组转换为xml
     * @param array $data 要转换的数组
     * @param bool $root 是否要根节点
     * @param bool $rootName 根节点名称
     * @return string         xml字符串
     */
    public static function arrayToXml($data, $root = true, $rootName = 'xml')
    {
        $str = "";
        if ($root) {
            $str .= "<{$rootName}>";
        }
        foreach ($data as $key => $val) {
            if (is_array($val)) {
                $child = self::arrayToXml($val, false);
                $str .= "<$key>$child</$key>";
            } else {
                $str .= "<$key>$val</$key>";
            }
        }
        if ($root) {
            $str .= "</{$rootName}>";
        }
        return $str;
    }
    
    /**
     * 将xml转换为数组
     * @return string xml字符串
     */
    public static function xmlToArray( string $xml)
    {
        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    }
    
    /**
     * 替换空格换行数据
     * @param string $string
     */
    public static function replaceUnnormalString(string $string)
    {
        return str_replace([PHP_EOL,' '],['',''],$string);
    }
    
    /**
     * 过滤字符串中emoji表情
     * @param $str string 昵称
     * @return string
     */
    public static function filterEmoji($str)
    {
        $str = preg_replace_callback('/./u', function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        }, $str);
        return $str;
    }
    
    /**
     * 获取订单微服务网关
     * @return string
     */
    public static function getOmsGateWay()
    {
        $url = config('app.api.yc_sys.API_OMS_GATWAY');
        return $url ? $url : 'http://pssapi.local';
    }
    
    /**
     * 是否打印供应商商品库存变化，商品变化日志
     */
    public static function isDebugSupplierGoodsConsumer()
    {
        return config('app.api.debug_supplier_goods_consumer');
    }
    
    /**
     * 获取日期时间
     */
    public static function getDateTimeStamp()
    {
        return date('Y-m-d H:i:s');
    }
    
    /**
     * 判断是否新版本bbc业务模式
     * @return string
     */
    public static function getIsNewBbcJavaMode()
    {
        $config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('new_bbc_java');
        $is_open_db = !empty($config['is_open_db']) && $config['is_open_db'] == 1 ? 1 : 0 ;
        return $is_open_db;
    }
}